package com.HMI;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import com.Resources.CustomColor;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@SuppressWarnings("serial")
public class ScheduleWindow extends AlphaPanel {

	private Window parent;
	private AlphaPanel schedulePanel;
	private JPanel background;
	private JPanel weekdaysPanel;
	private ScheduleRow monday;	
	private ScheduleRow tuesday;
	private ScheduleRow wednesday;
	private ScheduleRow thursday;
	private ScheduleRow friday;
	private ScheduleRow saturday;
	private ScheduleRow sunday;
	private List shockEvents = new ArrayList<Calendar>();
	private int week=0;
	
	public ScheduleWindow(int x, int y, int width, int height, Window parent) {
		super(x, y, width, height);
		this.setBackground(CustomColor.TRANSPARENT);
		this.parent = parent;
		initializeSchedulePanel();
	}

	private void initializeSchedulePanel() {
		schedulePanel = new AlphaPanel(40, 40, 502, 412);
		populateSchedulePanel();
		this.add(schedulePanel);
	}

	private void populateSchedulePanel() {
		background = new JPanel();
		background.setBounds(10, 48, 482, 320);
		background.setBackground(CustomColor.NAVY_BLUE);
		schedulePanel.add(background);
		background.setLayout(null);
		initializeWeekdaysPanel();
		getCurrentDate();
		addButtons();
	}
	
	private void addButtons() {
		MenuButton shockNow = new MenuButton("Shock Now",352,376,140,27);
		shockNow.setBackground(CustomColor.LIGHT_ORANGE);
		shockNow.setForeground(CustomColor.NAVY_BLUE);
		shockNow.setFont(new Font("DejaVu Sans", Font.PLAIN, 18));
		shockNow.setHorizontalAlignment(SwingConstants.CENTER);
		shockNow.setOpaque(true);
		schedulePanel.add(shockNow);
		
//		MenuButton prev = new MenuButton("Prev", 201,11,100,27);
//		prev.setOpaque(true);
//		prev.setHorizontalAlignment(SwingConstants.CENTER);
//		prev.setBackground(CustomColor.BLUE);
//		shockNow.setFont(new Font("DejaVu Sans", Font.PLAIN, 18));
//		prev.addActionListener(new ActionListener() {
//
//			@Override
//			public void actionPerformed(ActionEvent e) {
//				System.out.println("Previous clicked");
//				Calendar cal = Calendar.getInstance();
//				week--;
//				int days = week*7;
//				cal.add(Calendar.DATE, days);
//				getCurrentDate((Calendar) cal.clone());
//			}
//			
//		});
//		schedulePanel.add(prev);
//		
//		MenuButton next = new MenuButton("Next",201,376,100,27);
//		next.setOpaque(true);
//		next.setHorizontalAlignment(SwingConstants.CENTER);
//		next.setBackground(CustomColor.BLUE);
//		shockNow.setFont(new Font("DejaVu Sans", Font.PLAIN, 18));
//		schedulePanel.add(next);
	}

	private void initializeWeekdaysPanel() {
		weekdaysPanel = new JPanel();
		weekdaysPanel.setBounds(1, 1, 77, 318);
		weekdaysPanel.setBackground(CustomColor.BLUE);
		background.add(weekdaysPanel);
		weekdaysPanel.setLayout(null);
		PanelText monday = new PanelText("Mon", 0, 0,
				77, 45, SwingConstants.CENTER, SwingConstants.CENTER);
		monday.setForeground(CustomColor.WHITE);
		weekdaysPanel.add(monday);
		PanelText tuesday = new PanelText("Tue", 0, 45,
				77, 46, SwingConstants.CENTER, SwingConstants.CENTER);
		tuesday.setForeground(CustomColor.WHITE);
		weekdaysPanel.add(tuesday);
		PanelText wednesday = new PanelText("Wed", 0, 91,
				77, 45, SwingConstants.CENTER, SwingConstants.CENTER);
		wednesday.setForeground(CustomColor.WHITE);
		weekdaysPanel.add(wednesday);
		PanelText thursday = new PanelText("Thu", 0, 136,
				77, 46, SwingConstants.CENTER, SwingConstants.CENTER);
		thursday.setForeground(CustomColor.WHITE);
		weekdaysPanel.add(thursday);
		PanelText friday = new PanelText("Fri", 0, 182,
				77, 45, SwingConstants.CENTER, SwingConstants.CENTER);
		friday.setForeground(CustomColor.WHITE);
		weekdaysPanel.add(friday);
		PanelText saturday = new PanelText("Sat", 0, 227,
				77, 46, SwingConstants.CENTER, SwingConstants.CENTER);
		saturday.setForeground(CustomColor.WHITE);
		weekdaysPanel.add(saturday);
		PanelText sunday = new PanelText("Sun", 0, 273,
				77, 45, SwingConstants.CENTER, SwingConstants.CENTER);
		sunday.setForeground(CustomColor.WHITE);
		weekdaysPanel.add(sunday);
	}
	
	private void getCurrentDate() {
		Calendar cal = Calendar.getInstance();
		String weekday = new SimpleDateFormat("EEE").format(Calendar.getInstance().getTime());
		switch(weekday){
			case "Mon":
				populateDates(cal);
				break;
			case "Tue":
				cal.add(Calendar.DATE, -1);
				populateDates(cal);
				break;
			case "Wed":
				cal.add(Calendar.DATE, -2);
				populateDates(cal);
				break;
			case "Thu":
				cal.add(Calendar.DATE, -3);
				populateDates(cal);
				break;
			case "Fri":
				cal.add(Calendar.DATE, -4);
				populateDates(cal);
				break;
			case "Sat":
				cal.add(Calendar.DATE, -5);
				populateDates(cal);
				break;
			case "Sun":
				cal.add(Calendar.DATE, -6);
				populateDates(cal);
				break;
		}
		highlightCurrent();
	}
	
	private void getCurrentDate(Calendar cal) {
		System.out.println("Get Current Date Called: " + new SimpleDateFormat("EEE, MMM d").format(cal.getTime()));
		monday.removeRow();
		tuesday.removeRow();
		wednesday.removeRow();
		thursday.removeRow();
		friday.removeRow();
		saturday.removeRow();
		sunday.removeRow();
		String weekday = new SimpleDateFormat("EEE").format(cal.getTime());
		switch(weekday){
			case "Mon":
				populateDates(cal);
				break;
			case "Tue":
				cal.add(Calendar.DATE, -1);
				populateDates(cal);
				break;
			case "Wed":
				cal.add(Calendar.DATE, -2);
				populateDates(cal);
				break;
			case "Thu":
				cal.add(Calendar.DATE, -3);
				populateDates(cal);
				break;
			case "Fri":
				cal.add(Calendar.DATE, -4);
				populateDates(cal);
				break;
			case "Sat":
				cal.add(Calendar.DATE, -5);
				populateDates(cal);
				break;
			case "Sun":
				cal.add(Calendar.DATE, -6);
				populateDates(cal);
				break;
		}
		if(week != 0) {
			monday.setBackground(Color.WHITE);
			tuesday.setBackground(Color.WHITE);
			wednesday.setBackground(Color.WHITE);
			thursday.setBackground(Color.WHITE);
			friday.setBackground(Color.WHITE);
			saturday.setBackground(Color.WHITE);
			sunday.setBackground(Color.WHITE);
		} else {
			highlightCurrent();
		}
	}
	
	private void highlightCurrent() {
		Calendar cal = Calendar.getInstance();
		String weekday = new SimpleDateFormat("EEE").format(cal.getTime());
		switch(weekday){
			case "Mon":
				monday.setBackground(CustomColor.LIGHT_ORANGE);
				sunday.setBackground(Color.WHITE);
				break;
			case "Tue":
				tuesday.setBackground(CustomColor.LIGHT_ORANGE);
				monday.setBackground(Color.WHITE);
				break;
			case "Wed":
				wednesday.setBackground(CustomColor.LIGHT_ORANGE);
				tuesday.setBackground(Color.WHITE);
				break;
			case "Thu":
				thursday.setBackground(CustomColor.LIGHT_ORANGE);
				wednesday.setBackground(Color.WHITE);
				break;
			case "Fri":
				friday.setBackground(CustomColor.LIGHT_ORANGE);
				thursday.setBackground(Color.WHITE);
				break;
			case "Sat":
				saturday.setBackground(CustomColor.LIGHT_ORANGE);
				friday.setBackground(Color.WHITE);
				break;
			case "Sun":
				sunday.setBackground(CustomColor.LIGHT_ORANGE);
				saturday.setBackground(Color.WHITE);
				break;
		}
	}
	
	private void populateDates(Calendar cal) {
		monday = new ScheduleRow(1, 45, background, (Calendar) cal.clone(), this);
		cal.add(Calendar.DATE, +1);
		tuesday = new ScheduleRow(47, 45, background, (Calendar) cal.clone(), this);
		cal.add(Calendar.DATE, +1);
		wednesday = new ScheduleRow(93, 45, background, (Calendar) cal.clone(), this);
		cal.add(Calendar.DATE, +1);
		thursday = new ScheduleRow(139, 45, background, (Calendar) cal.clone(), this);
		cal.add(Calendar.DATE, +1);
		friday = new ScheduleRow(185, 44, background, (Calendar) cal.clone(), this);
		cal.add(Calendar.DATE, +1);
		saturday = new ScheduleRow(230, 44, background, (Calendar) cal.clone(), this);
		cal.add(Calendar.DATE, +1);
		sunday = new ScheduleRow(275, 44, background, (Calendar) cal.clone(), this);
	}
	
	public void updateDates() {
		monday.removeRow();
		tuesday.removeRow();
		wednesday.removeRow();
		thursday.removeRow();
		friday.removeRow();
		saturday.removeRow();
		sunday.removeRow();
		getCurrentDate();
	}
	
	public void showPopup(String weekday, String month, String date, String hour, String minute, String ampm, Calendar calendar, boolean shockScheduled) {
		parent.showPopup(weekday, month, date, hour, minute, ampm, calendar, shockScheduled);
	}
	
	public void addShockEvent(Calendar calendar) {
		shockEvents.add(calendar);
		for(int i=0; i<shockEvents.size(); i++) {
			System.out.println(new SimpleDateFormat("EEE, MMM, d  h:mm a").format(((Calendar) shockEvents.get(i)).getTime()));
		}
		System.out.println("");
		updateDates();
	}
	
	public void removeShockEvent(Calendar calendar) {
		System.out.println("REMOVE SHOCK: " + new SimpleDateFormat("EEE, MMM, d  h:mm a").format(calendar.getTime()));
		
		shockEvents.remove(calendar);
		for(int i=0; i<shockEvents.size(); i++) {
			if(new SimpleDateFormat("EEE, MMM, d  h:mm a").format(((Calendar) shockEvents.get(i)).getTime())
					.equals(new SimpleDateFormat("EEE, MMM, d  h:mm a").format(calendar.getTime()))) {
				shockEvents.remove(i);
			}
		}
		for(int i=0; i<shockEvents.size(); i++) {
			System.out.println(new SimpleDateFormat("EEE, MMM, d  h:mm a").format(((Calendar) shockEvents.get(i)).getTime()));
		}
		
		System.out.println("");
		updateDates();
	}
	
	public String getShockTime(String date) {
		for(int i=0; i<shockEvents.size(); i++) {
			String shock = new SimpleDateFormat("EEE, MMM, d").format(((Calendar) shockEvents.get(i)).getTime());
			if(shock.equals(date)) {
				return new SimpleDateFormat("h:mm a").format(((Calendar) shockEvents.get(i)).getTime());
			}
		}
		return "";
	}
}
